package com.longdaji.decoration.ui.activitiesOfCommunity.publishDynamic.addTopic;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.TopicAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Topic;
import com.longdaji.decoration.utils.SearchUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AddTopicActivity extends BaseActivity implements AddTopicContract.View {

    @Inject
    AddTopicContract.Presenter mPresenter;
    @BindView(R.id.et_search_content)
    EditText etSearchContent;
    @BindView(R.id.iv_delete)
    ImageView ivDelete;
    @BindView(R.id.rv_topic)
    RecyclerView rvTopic;

    private TopicAdapter mAdapter;
    private List<Topic> mTopicList = new ArrayList<>();
    private String mSearchContent = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_topic);
        setUnBinder(ButterKnife.bind(this));

        if (mPresenter != null)
            mPresenter.setView(this);

        mAdapter = new TopicAdapter(mTopicList);
        mAdapter.setOnItemClickListener(new TopicAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                etSearchContent.setText(mTopicList.get(position).getTopic());
            }
        });
        rvTopic.setAdapter(mAdapter);
        rvTopic.setLayoutManager(new LinearLayoutManager(this));

        etSearchContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                mSearchContent = editable.toString();
                etSearchContent.setSelection(mSearchContent.length());
                mPresenter.onAfterTextChanged(mSearchContent);
                mAdapter.setData(SearchUtil.searchTopic(mSearchContent, mTopicList));
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.tv_finish, R.id.iv_delete})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_finish:
                mPresenter.onFinishClick(etSearchContent.getText().toString());
                finish();
                break;
            case R.id.iv_delete:
                etSearchContent.setText("");
            default:
                break;
        }
    }

    @Override
    public void showDeleteInvisible() {
        ivDelete.setVisibility(View.INVISIBLE);
    }

    @Override
    public void showDeleteVisible() {
        ivDelete.setVisibility(View.VISIBLE);
    }
}
